package com.zx.erp.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.erp.sys.bean.*;
import com.zx.erp.sys.service.DeptService;
import com.zx.erp.sys.service.JobService;
import com.zx.erp.sys.service.ModuleService;
import com.zx.erp.sys.service.PermissionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者信息
 * @since 2021-11-10
 */
@Controller
@RequestMapping("/sys/job")
@SessionAttributes({"pageInfo"})
public class JobController {
    @Autowired
    private JobService jbiz;
    @Autowired
    private DeptService dbiz;
    @Autowired
    private ModuleService mbiz;
    @Autowired
    private PermissionService pbiz;

    @RequestMapping("/findByPage")
//    @MyLog("查询所有职位操作")
    public String findByPage(Model model, Job job, @RequestParam(value = "pageno", defaultValue = "1") Integer pageNo) {
        Integer pageSize = 6;
        Page<Job> pageInfo = jbiz.findByPage(job, pageNo, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("job", job);
        //查询所有部门信息
        List<Dept> dlist = dbiz.list();
        model.addAttribute("dlist", dlist);
        return "forward:/sys/dept/positionList.html";
    }


    // 授权 操作

    /**
     * 进入授权页面
     *
     * @return
     */
    @RequestMapping("/grantPage/{jobId}")
    public String toGrantPage(Model model, HttpSession session, @PathVariable("jobId") Integer jobId) {
        //左侧边栏 状态是0 所有
        List<Module> modules = mbiz.selectAllModule2("", 1);
        //显示所有父模块 状态是0 所有
        List<Module> allParentModule = mbiz.findAllParentModule2(1);
        model.addAttribute("modules", modules);
        model.addAttribute("pmodules", allParentModule);
        model.addAttribute("jobId", jobId);
        return "/sys/dept/positionGrant";
    }


    //选中一级菜单
    @RequestMapping("/ajaxGrant1/{jobId}")
    @ResponseBody
    public List<Module> initGrantPage1(@PathVariable("jobId") Integer jobId) {
        //状态设置为0  1级模块
        List<Module> pModule = pbiz.findMoudleByJob(jobId, 0, 1);
        return pModule;
    }

    //选中二级菜单
    @RequestMapping("/ajaxGrant2/{jobId}")
    @ResponseBody
    public List<Module> initGrantPage2(@PathVariable("jobId") Integer jobId) {
        //状态设置为0  2级模块
        List<Module> cModule = pbiz.findMoudleByJob(jobId, 0, 2);
        return cModule;
    }


    @RequestMapping("/updatePosition")
    public String updatePosition(@Param("jobId") Integer jobId, @Param("mids") String mids, Model model) {
        List<Permission> permissions = new ArrayList<Permission>();
        if (mids != null && !mids.equals("") && mids.length() != 0) {
            String[] midArr = mids.split(",");
            for (int i = 0; i < midArr.length; i++) {
                Permission perm = new Permission();
                perm.setComrid(jobId);
                perm.setCompid(Integer.parseInt(midArr[i]));
                perm.setComstate(1);
                permissions.add(perm);
            }
        }
        Boolean flag = pbiz.grantPermission(jobId, permissions);
        Page<Users> pageInfo = (Page<Users>) model.getAttribute("pageInfo");
        Long pageno = pageInfo.getCurrent();
        return "redirect:/sys/job/findByPage?pageno=" + pageno;
    }
}

